//
//  BKHeZhaoFlowLaout.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/6/4.
//  Copyright (c) 2015年 宏图教育. All rights reserved.
//

#import "BKHeZhaoFlowLaout.h"


@interface BKHeZhaoFlowLaout(){

    NSInteger _totalNumber;
    CGRect _rect;
}

@end

@implementation BKHeZhaoFlowLaout

- (void)prepareLayout{
    [super prepareLayout];
    _totalNumber =  [self.collectionView numberOfItemsInSection:0];
    _rect = self.collectionView.frame;
  

}



- (CGSize)collectionViewContentSize
{
    return CGSizeMake(_rect.size.width, _totalNumber/4*(_rect.size.width-40)/4*1.5+( _totalNumber/4-1)*10);
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    NSInteger col = indexPath.row%4 ;
    NSInteger row = indexPath.row/4;
    
    
    
    CGPoint center = CGPointMake(5+(_rect.size.width-30)/8+col*(_rect.size.width)/4,(_rect.size.width)/8*1.5+row*((_rect.size.width-40)/4*1.5+10));
    
    
    NSInteger rows = indexPath.row/4;//多少行
    NSInteger cols = indexPath.row%4;//第几列
    
    NSInteger rowNO = rows%4; //四行一个循环
    switch (rowNO) {
        case 0://第一行
        {
            switch (cols) {
                case 0:
                    center.y +=20;
                    break;
                case 1:
                    break;
                case 2:
                    center.y+=10;
                    break;
                case 3:
                    center.y +=30;
                    break;
                default:
                    break;
            }
            
        }
            break;
        case 1:
        {
            switch (cols) {
                case 0:{
                
//                    center.x+=10;
                    CGAffineTransform transform = CGAffineTransformMakeScale(.9, .9);
                    attributes.transform = transform;
                    center.y +=30;
                }
                    
                    break;
                case 1:{
                    CGAffineTransform transform = CGAffineTransformMakeScale(.8, .8);
                    attributes.transform = transform;

                }
                    break;
                case 2:{
                    center.y +=10;
                    CGAffineTransform transform = CGAffineTransformMakeScale(.9, 0.9);
                    attributes.transform = transform;

                
                }
                    break;
                case 3:{
                
                    center.y+=10;
                    CGAffineTransform transform = CGAffineTransformMakeScale(1.1, .7);
                    attributes.transform = transform;
                    
                }
                    break;
                default:
                    break;
            }
            
        }
            break;
        case 2:{
            switch (cols) {
                case 0:{
                
                    center.y+=20;
                    CGAffineTransform transform = CGAffineTransformMakeScale(.8, .7);
                    attributes.transform = transform;
                }
                    
                    break;
                case 1:{
                    CGAffineTransform transform = CGAffineTransformMakeScale(1.2, 1.2);
                    attributes.transform = transform;
                
                }
                    break;
                case 2:{
                
                    CGAffineTransform transform = CGAffineTransformMakeScale(.9, .9);
                    attributes.transform = transform;

                }
                    break;
                case 3:
                    break;
                default:
                    break;
            }

        }
            break;
        case 3:{
            switch (cols) {
                case 0:
                    
                    break;
                case 1:{
                
                    CGAffineTransform transform = CGAffineTransformMakeScale(.8, .8);
                    attributes.transform = transform;
                }
                    break;
                case 2:{
                    center.x-=10;
                    center.y+=10;
                
                
                    
                }
                    break;
                case 3:{
                
                    center.y+=10;
                    CGAffineTransform transform = CGAffineTransformMakeScale(1.2, 1.2);
                    attributes.transform = transform;
                }
                    break;
                default:
                    break;
            }

        }
            break;
        default:
            break;
    }
    
    attributes.center = center;
    attributes.size = CGSizeMake((_rect.size.width-40)/4,(_rect.size.width-40)/4*1.5);
    
    
    NSArray *angles = @[@10,@90,@(-60),@10,@10,@(-20)];
    
    
    CGFloat angele = [angles[indexPath.row%6] floatValue];
    
    CGFloat scale = arc4random()%5*3/10.0;
    
    
    CGAffineTransform transform = CGAffineTransformMakeScale(scale, scale);
//    attributes.transform = transform;
    return attributes;
}

-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSArray *arr = [super layoutAttributesForElementsInRect:rect];
    if ([arr count] > 0) {
        return arr;
    }
    NSMutableArray *attributes = [NSMutableArray array];
    for (NSInteger i = 0 ; i < [self.collectionView numberOfItemsInSection:0 ]; i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        [attributes addObject:[self layoutAttributesForItemAtIndexPath:indexPath]];
    }
    return attributes;
}


@end
